<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      class="from-panel"
    >
      <h1>Saas经销商业务管理系统</h1>
      <span>Saas Dealer Management System</span>
      <el-form-item prop="username">
        <el-input
          prefix-icon="el-icon-user"
          v-model="ruleForm.username"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          prefix-icon="el-icon-lock"
          v-model="ruleForm.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          class="btn"
          @click="submitForm('ruleForm')"
          :loading="loading"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from "vuex";

export default {
  data() {
    let valiName = (rule, value, callback) => {
      if (/^\w{11}/.test(value)) {
        return callback();
      }
      callback(new Error("账号只能是 数字字母下划线"));
    };
    let valiPass = (rule, value, callback) => {
      if (/^\w{1,12}/.test(value)) {
        return callback();
      }
      callback(new Error("密码只能是5-12的数值字母下划线"));
    };
    return {
      loading: false,
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 11, max: 11, message: "用户名长度在11位", trigger: "blur" },
          { validator: valiName, trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: valiPass, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapActions("userStore", ["loginTo"]),
    submitForm(ruleForm) {
      let path = this.$route.query.toPath;
      console.log(path);
      this.$refs[ruleForm].validate(async (vali) => {
        if (vali) {
          this.loading = true;
          try {
            await this.loginTo({
              phone: this.ruleForm.username,
              pwd: this.ruleForm.password,
            });
            this.loading = false;
            this.$message.success("登录成功");
            console.log("toPath",this.$router.options.routes)
            this.$router.replace({ path: path || "/" });
          } catch (e) {
            console.log(e);
            this.$message.error(e?.msg || "账号密码错误");
            this.loading = false;
          }
        } else {
          this.$message.error("请输入正确的账号密码");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/sass/var.scss";
.from-panel {
  width: 420px;
  height: 400px;
  box-sizing: border-box;
  padding: 40px 50px;
  margin: 0 auto;
  margin-top: 30px;
  background-color: #fff;
  border-radius: 15px;
  &::v-deep input {
    height: 50px;
  }
}
h1 {
  color: $panel-color;
  font-size: 28px;
}
span {
  color: $panel-color;
  font-size: 18px;
  display: inline-block;
  padding-bottom: 30px;
}
.btn {
  background-color: $panel-color;
  width: 100%;
  height: 50px;
}
</style>